<template>
  <div>
    <div v-show="$route.name == 'material-allocation-list'">
      <!-- 面包屑导航区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>调拨物资记录</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片视图区域 -->
      <el-card>
        <div style="margin-bottom: 20px;">调拨物资记录</div>
        <!-- 搜索与添加区域 -->
        <el-row class="query">
          <el-col :span="24">
            <el-button type="success" @click="addAllocation" style="margin-bottom: 10px">新增物资调拨</el-button>
          </el-col>
        </el-row>
        <el-row class="query">
          <el-col :span="24">
            <el-select v-model="searchList.base" popper-class="dropdownbox" style="width:200px" placeholder="请选择调拨门店" multiple>
              <el-option v-for="item in storeList" :label="item.basename" v-if="item.base != base"
                :value="item.base" :key="item.base">
                <span style="float: left">{{ item.basename }}</span>
              </el-option>
            </el-select>
            <el-button type="primary" @click="search" style="margin-left: 5px">查询</el-button>
            <el-button style="margin-left: 5px"  @click="reset">重置</el-button>
          </el-col>
        </el-row>
        <el-table
          border
          v-loading="loading"
          :data="allocationData"
          style="margin-top: 10px;">
          <el-table-column label="申请日期" prop="allocation_time" min-width="110" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.allocation_time ? new Date(scope.row.allocation_time).toLocaleDateString().split('/').join('-') : '' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="调拨门店" prop="base_from_name" min-width="80" align="center"></el-table-column>
          <el-table-column label="调拨申请号" prop="apply_number" min-width="110" align="center"></el-table-column>
          <el-table-column label="状态" prop="status" min-width="80" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.status === 1">已完成</span>
              <span v-else-if="scope.row.status === 2">待审批</span>
              <span v-else-if="scope.row.status === 3">仓管审核中</span>
              <span v-else-if="scope.row.status === 4">驳回</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="170" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="clickButton(1, scope.row.id)">详情</el-button>
              <el-button size="mini" type="warning" @click="clickButton(2, scope.row.id)" v-if="scope.row.status === 2 || scope.row.status === 4" style="margin-left: 10px;">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
      </el-card>
    </div>
    <div>
      <router-view />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchList: {
        base: []
      },
      base: '',
      storeList: [],
      allocationData: [],
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      loading: false,
    }
  },
  watch: {
    $route(now, old) {
      if (old.name === 'material-allocation-save' || old.name === 'material-allocation-detail' || old.name === 'material-allocation-update') {
        this.getAllocateList()
      }
    }
  },
  mounted() {
    this.getStoreList()
    this.getUserInfo()
  },
  methods: {
    addAllocation() {
      this.$router.push({ 'name': 'material-allocation-save' })
    },
    // 获取用户信息
    async getUserInfo() {
      const { data: result } = await this.$http.get('/user/' + localStorage.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.base = result.data.base ? result.data.base * 1 : ''
      await this.getAllocateList()
    },
    // 获取门店
    async getStoreList() {
      const { data: result } = await this.$http.get('/store/all')
      if (result.status !== 200) return this.$message.error(result.msg)
      this.storeList = result.data
    },
    reset() {
      this.searchList = {
        base: []
      },
      this.allocationData = []
      this.pager.currPage = 1
      this.getAllocateList()
    },
    search() {
      this.allocationData = []
      this.pager.currPage = 1
      this.getAllocateList()
    },
    async getAllocateList() {
      this.loading = true
      const params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
        status: [1,2,3,4],
        base_to: [this.base],
        base_from: this.searchList.base
      }
      const { data: result } = await this.$http.get('/allocation', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.allocationData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.allocationData = result.data
      this.pager = result.pager
    },
    clickButton(type, id) {
      if (type === 1) {
        this.$router.push({
          name: 'material-allocation-detail',
          params: { allocate_id: id, come: 'material-allocation-list'}
        })
      } else {
        this.$router.push({
          name: 'material-allocation-update',
          params: { allocate_id: id, come: 'material-allocation-list'}
        })
      }
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getAllocateList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getAllocateList()
    },
  }
}
</script>